<?php
namespace app\index\home;
use app\common\controller\Common;
use think\Db;

class Purchase extends Common
{
    /**
     * 我要补货
     */
    public function wybh(){
        if($this->request->isPost()){
            $data = $this->request->post();
            if($data['cggs'] == '' || $data['cggs'] == 0){
                return $this->error('采购数量不能为0');
            }
            if($data['zzpz'] == ''){
                return $this->error('请上传转账凭证');
            }
            $data['pay_status'] = 0;
            $data['xdsj'] = time();
            $data['dl_id'] = session('index_daili.id'); 
            if (Db::name("cgjl")->insert($data)) {
                return $this->success('添加成功，等待系统审核！');
            }else{
                return $this->error('添加失败');
            } 
        }else{
            $daili_id = session('index_daili.id');
            $daili_info = Db::name('daili')->where("id = '{$daili_id}'")->find();
            $dldj_info = Db::name("dldj")->where("id = '{$daili_info['lddj_id']}'")->find();
            $this->assign("dldj_info",$dldj_info);
            $this->assign("daili_info",$daili_info);
            return $this->fetch();
        }
    }
    
    /**
     * 补货记录
     */
    public function bhjl(){
        $daili_id = session('index_daili.id');
        $bh_list = Db::name("cgjl")->where("dl_id = '{$daili_id}'")->select();
        $this->assign("bh_list",$bh_list);
        return $this->fetch();
    }
    
    /**
     * 我要录单
     */
    public function wyld(){
        if($this->request->isPost()){
            $data = $this->request->post();
            if($data['name'] == ''){
                return $this->error('姓名不能为空');
            }
            if($data['phone'] == ''){
                return $this->error('电话不能为空');
            }
            if($data['gsmc'] == ''){
                return $this->error('公司名称不能为空');
            }
            if($data['gsdz'] == ''){
                return $this->error('公司地址不能为空');
            }
            
            $data['scsj'] = time(); 
            $data['dl_id'] = session('index_daili.id');
            $system_user = Db::name("system_user")->where("role_id = 4")->column("id");
            $key_rand = array_rand($system_user);
            $data['kf_id'] = $system_user[$key_rand];
            if (Db::name("user")->insert($data)) {
                //注册成功，代理系统个数减一（初始代理不减）
                $dqdl_id = session('index_daili.id');
                if($dqdl_id != 1){
                    Db::name('daili')->where("id = '{$dqdl_id}'")->setDec('xtgs');
                } 
                return $this->success('录单成功，等待安装！');
            }else{
                return $this->error('录单失败');
            } 
        }else{ 
            return $this->fetch();
        }
    }
    
    /**
     * 订单记录
     */
    public function ddjl(){
        $daili_id = session('index_daili.id');
        $dd_list = Db::name("user")->where("dl_id = '{$daili_id}'")->select();
        $this->assign("dd_list",$dd_list);
        return $this->fetch();
    }
}
